home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_05 / pjp / tfstream.c < prev   
Encoding:
C/C++ Source or Header  |  1995-04-02  |  3.9 KB  |  97 lines

  1. ------------- Listing 9: The file tfstream.c ------------------
  2.  
  3. // test <fstream>
  4. #include <cassert>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <fstream>
  8. #include <iostream>
  9.  
  10. int main()
  11.         {       // test basic workings of fstream definitions
  12.         ifstream ifs;
  13.         ofstream ofs;
  14.         const char *tn = tmpnam(NULL);
  15.         assert(tn != NULL);
  16.                 // test closed file closing
  17.         assert(!ifs.is_open() && !ifs.fail());
  18.         ifs.close();
  19.         assert(ifs.fail() && ifs.rdbuf()->close() == 0);
  20.         assert(!ofs.is_open() && !ofs.fail());
  21.         ofs.close();
  22.         assert(ofs.fail() && ofs.rdbuf()->close() == 0);
  23.                 // test output file operations
  24.         ofs.clear(), ofs.open(tn, ios::out | ios::trunc);
  25.         assert(ofs.is_open() && ofs.rdbuf()->is_open());
  26.         ofs << "this is a test" << endl;
  27.         ofs.close();
  28.         assert(!ofs.is_open() && ofs.good());
  29.         assert(ofs.rdbuf()->open(tn, ios::app | ios::out) != 0);
  30.         ofs << "this is only a test" << endl;
  31.         ofs.close();
  32.         assert(!ofs.is_open() && ofs.good());
  33.                 // test input file operations
  34.         char buf[50];
  35.         ifs.clear(), ifs.open(tn, ios::in);
  36.         assert(ifs.is_open() && ifs.rdbuf()->is_open());
  37.         ifs.getline(buf, sizeof (buf));
  38.         assert(strcmp(buf, "this is a test") == 0);
  39.         streampos p1 = ifs.rdbuf()->pubseekoff(0, ios::cur);
  40.         ifs.getline(buf, sizeof (buf));
  41.         assert(strcmp(buf, "this is only a test") == 0);
  42.         assert(ifs.rdbuf()->pubseekpos(p1) == p1);
  43.         ifs.getline(buf, sizeof (buf));
  44.         assert(strcmp(buf, "this is only a test") == 0);
  45.         ifs.rdbuf()->pubseekoff(0, ios::beg);
  46.         ifs.getline(buf, sizeof (buf));
  47.         assert(strcmp(buf, "this is a test") == 0);
  48.         ifs.close();
  49.         assert(!ifs.is_open() && ifs.good());
  50.                 // test combined file operations
  51.         ifstream nifs(tn, ios::in | ios::out);
  52.         ostream nofs(nifs.rdbuf());
  53.         assert(nifs.is_open() && nifs.good() && nofs.good());
  54.         nifs.rdbuf()->pubseekoff(0, ios::end);
  55.         nofs << "this is still just a test" << endl;
  56.         nifs.rdbuf()->pubseekoff(0, ios::beg);
  57.         nifs.getline(buf, sizeof (buf));
  58.         assert(strcmp(buf, "this is a test") == 0);
  59.         nifs.getline(buf, sizeof (buf));
  60.         assert(strcmp(buf, "this is only a test") == 0);
  61.         nifs.getline(buf, sizeof (buf));
  62.         assert(strcmp(buf, "this is still just a test") == 0);
  63.         nifs.close();
  64.         ofstream nnofs(tn,
  65.                 ios::in | ios::out | ios::ate);
  66.         assert(nnofs.is_open());
  67.         nnofs << "one last test" << endl;
  68.         nnofs.close();
  69.                 // test stdiobuf operations
  70.         FILE *fi = fopen(tn, "r+");
  71.          {      // bound lifetime of istd and ostd
  72.         istdiostream istd(fi);
  73.         ostdiostream ostd(fi);
  74.         assert(fi != 0);
  75.         assert(istd.buffered() == 0
  76.                 && istd.rdbuf()->buffered() == 0);
  77.         istd.rdbuf()->buffered(0), istd.buffered(1);
  78.         assert(istd.buffered() != 0
  79.                 && istd.rdbuf()->buffered() != 0);
  80.         assert(ostd.buffered() == 0
  81.                 && ostd.rdbuf()->buffered() == 0);
  82.         ostd.rdbuf()->buffered(0), ostd.buffered(1);
  83.         assert(ostd.buffered() != 0
  84.                 && ostd.rdbuf()->buffered() != 0);
  85.         istd.getline(buf, sizeof (buf));
  86.         assert(strcmp(buf, "this is a test") == 0);
  87.         p1 = istd.rdbuf()->pubseekoff(0, ios::end);
  88.         ostd << "still one more last test" << endl;
  89.         assert(ostd.rdbuf()->pubseekpos(p1) == p1);
  90.         istd.getline(buf, sizeof (buf));
  91.         assert(strcmp(buf, "still one more last test") == 0);
  92.          }
  93.         assert(fclose(fi) == 0 && remove(tn) == 0);
  94.         cout << "SUCCESS testing <fstream>" << endl;
  95.         return (0);
  96.         }
  97.